package com.zeji.androidadvance;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import com.zeji.androidadvance.activity.handler.HandlerTestActivity;
import com.zeji.androidadvance.activity.littleAd.LittleAdActivity;
import com.zeji.androidadvance.activity.lottie.CustomLottieActivity;
import com.zeji.androidadvance.activity.media.MediaPlayActivity;
import com.zeji.androidadvance.activity.opengl.OpenGlActivity;
import com.zeji.androidadvance.activity.textSelect.TextSelectActivity;
import com.zeji.androidadvance.activity.touchFollow.TouchFollowActivity;
import com.zeji.androidadvance.activity.videolist.VideoListActivity;
import com.zeji.androidadvance.utils.ActivityJumpUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

@SuppressLint("NonConstantResourceId")
public class MainActivity extends AppCompatActivity {
    @BindView(R.id.mainList)
    ListView mainList;
    @BindView(R.id.mainTv)
    TextView tvMain;

    private List<ItemBean> buttons;

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        initData();

        mainList.setAdapter(new MyAdapter(this, R.layout.item_main, buttons));
    }

    private void initData() {
        buttons = new ArrayList<>();
        buttons.add(new ItemBean("手指触摸跟随的控件", v -> ActivityJumpUtil.obtainBuilder(TouchFollowActivity.class).jump()));
        buttons.add(new ItemBean("小广告控件", v -> ActivityJumpUtil.obtainBuilder(LittleAdActivity.class).jump()));
        buttons.add(new ItemBean("opengl 学习", v -> ActivityJumpUtil.obtainBuilder(OpenGlActivity.class).jump()));
        buttons.add(new ItemBean("跳转其他app", v -> ActivityJumpUtil.obtainBuilder("android.intent.action.COMPACT_ANIM")
                .putExtra("btnName", "我是安卓进阶")
                .putExtra("app", getPackageName())
                .jump()
        ));
        buttons.add(new ItemBean("视频列表", v -> ActivityJumpUtil.obtainBuilder(VideoListActivity.class).jump()));
        buttons.add(new ItemBean("播放音乐", v -> ActivityJumpUtil.obtainBuilder(MediaPlayActivity.class).jump()));
        buttons.add(new ItemBean("计时器", v -> ActivityJumpUtil.obtainBuilder(HandlerTestActivity.class).jump()));
        buttons.add(new ItemBean("阅读选择", v -> ActivityJumpUtil.obtainBuilder(TextSelectActivity.class).jump()));
        buttons.add(new ItemBean("自定义 lottie", v -> ActivityJumpUtil.obtainBuilder(CustomLottieActivity.class).jump()));
    }

    private static class ItemBean {
        String text;
        View.OnClickListener onClickListener;

        public ItemBean(String text, View.OnClickListener onClickListener) {
            this.text = text;
            this.onClickListener = onClickListener;
        }
    }

    private static class MyAdapter extends ArrayAdapter<ItemBean> {
        private final int resourceId;

        public MyAdapter(@NonNull Context context, int resource, @NonNull List<ItemBean> objects) {
            super(context, resource, objects);
            resourceId = resource;
        }

        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            ItemBean itemBean = getItem(position);
            View view;
            ViewHolder viewHolder;
            if (convertView == null) {
                view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);
                viewHolder = new ViewHolder();
                viewHolder.tvText = view.findViewById(R.id.itemMainText);
                view.setTag(viewHolder);
            } else {
                view = convertView;
                viewHolder = (ViewHolder) view.getTag();
            }
            viewHolder.tvText.setText(itemBean.text);
            view.setOnClickListener(v -> itemBean.onClickListener.onClick(v));
            return view;
        }

        static class ViewHolder {
            TextView tvText;
        }
    }
}